找传奇、传世资源到传世资源站!

android 智能机器人 示例代码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

package com.imooc.mooo;import java.util.ArrayList;import java.util.Date;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.text.TextUtils;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;import android.widget.Toast;import com.imooc.mooo.bean.ChatMessage;import com.imooc.mooo.bean.ChatMessage.Type;import com.imooc.mooo.utils.HttpUtils;public class MainActivity extends Activity{private ListView mMsgs;private ChatMessageAdapter mAdapter;private List<ChatMessage> mDatas;private EditText mInputMsg;private Button mSendMsg;private Handler mHandler = new Handler(){public void handleMessage(android.os.Message msg){// 等待接收,子线程完成数据的返回ChatMessage fromMessge = (ChatMessage) msg.obj;mDatas.add(fromMessge);mAdapter.notifyDataSetChanged();mMsgs.setSelection(mDatas.size()-1);};};@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);initView();initDatas();// 初始化事件initListener();}private void initListener(){mSendMsg.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){final String toMsg = mInputMsg.getText().toString();if (TextUtils.isEmpty(toMsg)){Toast.makeText(MainActivity.this, "发送消息不能为空!",Toast.LENGTH_SHORT).show();return;}ChatMessage toMessage = new ChatMessage();toMessage.setDate(new Date());toMessage.setMsg(toMsg);toMessage.setType(Type.OUTCOMING);mDatas.add(toMessage);mAdapter.notifyDataSetChanged();mMsgs.setSelection(mDatas.size()-1);mInputMsg.setText("");new Thread(){public void run(){ChatMessage fromMessage = HttpUtils.sendMessage(toMsg);Message m = Message.obtain();m.obj = fromMessage;mHandler.sendMessage(m);};}.start();}});}private void initDatas(){mDatas = new ArrayList<ChatMessage>();mDatas.add(new ChatMessage("你好,小慕为您服务", Type.INCOMING, new Date()));mAdapter = new ChatMessageAdapter(this, mDatas);mMsgs.setAdapter(mAdapter);}private void initView(){mMsgs = (ListView) findViewById(R.id.id_listview_msgs);mInputMsg = (EditText) findViewById(R.id.id_input_msg);mSendMsg = (Button) findViewById(R.id.id_send_msg);}}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复